package mcommand;

import java.awt.Color;

import javax.swing.JWindow;
import javax.swing.UIManager;

import mcommand.MainPanel.MainPanelState;
import mcommand.SoundHandler;

/**
 * Creates a start menue for game instructions, highscore and to start the game
 * shows a welcome image and a title melody.
 *
 * @author Marcel, Christian, Klaus
 */
@SuppressWarnings("serial")
public class StartMenu extends JWindow {
    // sets the start menue size
    private static final int STARTMENU_SIZE_X = 600;
    private static final int STARTMENU_SIZE_Y = 300;

    // mainpanel and menu size
    private static final int MAINPANEL_SIZE_X = 500;
    private static final int MAINPANEL_SIZE_Y = 300;
    private static final int MENUPANEL_SIZE_X = 100;
    private static final int MENUPANEL_SIZE_Y = 300;

    // defines values for the animation steps and the strobe
    // effect frequency
    private static final int animationPixelSteps = 10;
    private static final int strobeEffectFrequency = 20;

    // defines variables for a MainPanel and a MenuPanel
    private MainPanel mainPanel;
    private MenuPanel menuPanel;

    // defines where the title melody can be found
    private static final String PATH_SOUND_STARTUP = "assets/startup.wav";
    
    // defines a SoundHandler for the startsound
    private final SoundHandler startSound;

    /**
     * The JWindow will be created and shown centered without captionline and
     * borders and the no ability to move.
     *
     * @param animation animation
     */
    public StartMenu(final boolean animation) {
        if (Board.paused != null) {
            Board.paused.stopSound();
        }
        // start playing the title sound of the game
        startSound = new SoundHandler(PATH_SOUND_STARTUP);
        startSound.playSound();

        // centers this on the screen
        setLocationRelativeTo(null);

        // sets that it has no default layout
        setLayout(null);

        // creates a new MainPanel
        mainPanel = new MainPanel();

        // places the MainPanel on the start menue window
        mainPanel.setLocation(0, 0);
        mainPanel.setSize(MAINPANEL_SIZE_X, MAINPANEL_SIZE_Y);

        // creates a new MenuPanel
        menuPanel = new MenuPanel(this, mainPanel);
        // places the MainPanel on the start menue window
        menuPanel.setLocation(STARTMENU_SIZE_X - MENUPANEL_SIZE_X, 0);
        menuPanel.setSize(MENUPANEL_SIZE_X, MENUPANEL_SIZE_Y);

        // joins the mainpanel with the menupanel
        mainPanel.setMenuPanel(menuPanel);

        // Sets the background color
        setBackground(Color.BLACK);

        //makes the frame visible
        setVisible(true);

        // animates the start menu
        if (animation) {
            //centers it
            setLocationRelativeTo(null);

            //makes the window bigger till the destinated size
            for (int i = 0; i <= STARTMENU_SIZE_X; i += animationPixelSteps) {

                //sets the size for the current round
                setSize(i, (int) (i / 2));

                // nice strobe effect
                if (i % strobeEffectFrequency == 0) {
                    setVisible(true);
                }

                // centers it
                setLocationRelativeTo(null);
                //makes it invisible so that just every 10
                //rounds the window can be seen (strobe effect)
                setVisible(false);
            }

          // without animation
        } else {
            setSize(STARTMENU_SIZE_X, STARTMENU_SIZE_Y);
            setLocationRelativeTo(null);
        }

        // to avoid grey screen
        setVisible(false);
        addComponents();
        setVisible(true);
    }

    /**
     * Adds the Components to the window.
     */
    public final void addComponents() {
        add(menuPanel);
        add(mainPanel);
    }

    /**
     *
     * If you create a new start menu with a MainPanelState,
     * the animation effect is default false.
     * @param state defines the startstate for MainPanel
     * (used on main games end)
     *
     */
    public StartMenu(final MainPanelState state) {
        this(false);

        mainPanel.setState(state);
    }

    /**
     * when the program starts a new StartMenu will be created.
     *
     * @param args
     *            console arguments
     */
    public static void main(final String[] args) {
        try {
            // tries to load a special look
            UIManager.setLookAndFeel("com.sun.java.swing.plaf"
            		+ ".nimbus.NimbusLookAndFeel");
        } catch (Exception e) {
            // prints out if something went wrong
            e.printStackTrace();
        }
        // new start menu
        new StartMenu(true);

    }

    /**
     * Starts the game, stops the title melody disables the start menu and makes
     * it invisible.
     */
    public final void startGame() {
        // hides the start menu
        this.setVisible(false);
        // stops the title melody
        startSound.stopSound();
        // creates a new Missile Command Game
        new MCommand();
        // disables this start menu
        this.setEnabled(false);
    }

}
